home *** CD-ROM | disk | FTP | other *** search
- /*
- ** FROM.C [edit EMAIL.H before compiling]
- **
- ** This program displays the header from each email message
- ** on the SMTP server. This is the GUI equivalent of the console
- ** mode example STATUS.C.
- **
- ** NOTES:
- ** This program disables the automatic calling of the driver.
- ** Thus, seeDriver must be called after every call to the SEE
- ** function library. This was done to integrate the functioning
- ** of SEE with the Windows event loop and to demonstrate the
- ** explicit use of seeDriver.
- */
-
- #include <windows.h>
- #include <winsock.h>
-
- #include "see.h"
- #include "message.h"
- #include "paint.h"
- #include "about.h"
- #include "str.h"
-
- #include "email.h"
-
- #ifdef WIN32
- #define USE_INS HINSTANCE
- #define USE_PTR PSTR
- #else
- #define USE_INS HANDLE
- #define USE_PTR LPSTR
- #endif
-
- #define POST_MSG(m) PostMessage(hMainWnd,WM_USER,(m),0)
- ///#define POST_PARM(m,p) PostMessage(hMainWnd,WM_USER,(m),(p))
-
- #define QUOTE 0x22
-
- LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
- HWND hMainWnd; /* main window handle */
-
- #define BUFF_SIZE 2048
- #define TEMP_SIZE 128
- #define STR_SIZE 50
-
- #define CMD_NONE 0
- #define CMD_CONNECT 1
- #define CMD_GET_COUNT 2
- #define CMD_READY 3
- #define CMD_NEXT_MSG 4
- #define CMD_GET_HEADERS 5
- #define CMD_CLOSE 6
- #define CMD_DRIVER 7
-
- static USE_INS hInstance;
- static int WinWidth = 8 * NCOLS; /* window width */
- static int WinHeight = 15 * NROWS; /* window height */
- static char Temp[TEMP_SIZE]; /* temporary buffer */
- static char Buffer[BUFF_SIZE]; /* buffer for headers */
- static HCURSOR ArrowCursor; /* arrow cursor */
- //static HCURSOR WaitCursor; /* hour glass cursor */
- static int NextCommand = CMD_NONE; /* next command to execute */
- static int MessageCount; /* number of messages */
- static int MessageNbr; /* current message number [1,MessageCount] */
- static char Pop3String[STR_SIZE] = POP3_HOST_NAME; /* POP3 server name*/
- static char UserString[STR_SIZE] = POP3_USER_NAME; /* POP3 user name */
- static char PassString[STR_SIZE] = POP3_PASSWORD; /* POP3 password */
-
- /* display error message */
-
- static void DisplayError(int Code, LPSTR Msg)
- {DisplayString("ERROR: ");
- if(Msg) DisplayString(Msg);
- if(Code)
- {seeErrorText(Code,(LPSTR)Temp,50);
- DisplayLine((LPSTR)Temp);
- }
- /* restore arrow cursor */
- SetCursor(ArrowCursor);
- }
-
- /* display email parameters */
-
- void ShowINI(void)
- {wsprintf((LPSTR)Temp," Server : %c%s%c", QUOTE,(LPSTR)Pop3String,QUOTE);
- DisplayLine((LPSTR)Temp);
- wsprintf((LPSTR)Temp," User : %c%s%c", QUOTE,(LPSTR)UserString,QUOTE);
- DisplayLine((LPSTR)Temp);
- wsprintf((LPSTR)Temp,"Password : %c%s%c", QUOTE,(LPSTR)PassString,QUOTE);
- DisplayLine((LPSTR)Temp);
- }
-
- /* WinMain */
-
- #ifdef WIN32
- int WINAPI
- #else
- int PASCAL
- #endif
- WinMain(USE_INS hInst, USE_INS hPrevInstance,
- USE_PTR szCmdLine, int nCmdShow)
- {WNDCLASS wc;
- MSG msg;
- BOOL Result;
- if(!hPrevInstance)
- {/* register main window class */
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = MainWndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInst;
- wc.hIcon = LoadIcon(hInst, "HostIcon");
- wc.hCursor = NULL;
- wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- wc.lpszMenuName = "HostMenu";
- wc.lpszClassName = "HostWClass";
- Result = RegisterClass(&wc);
- if(!Result) return FALSE;
- }
- /* create main window */
- hInstance = hInst;
- hMainWnd = CreateWindow(
- "HostWClass", "From", WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT,
- WinWidth, WinHeight,
- NULL, NULL,
- hInstance, NULL);
- ShowWindow(hMainWnd, nCmdShow);
- UpdateWindow(hMainWnd);
-
- /* window control loop */
-
- while(GetMessage(&msg,NULL,0,0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (msg.wParam);
- } /* end WinMain */
-
- #ifdef WIN32
- LRESULT CALLBACK
- #else
- long FAR PASCAL
- #endif
- MainWndProc(HWND hWindow,UINT iMsg,WPARAM wParam,LPARAM lParam)
- {int n, Code;
- HDC hDC;
- PAINTSTRUCT ps;
- #ifdef WIN32
- #else
- static FARPROC lpfnAboutDlgProc;
- #endif
- hMainWnd = hWindow;
- switch (iMsg)
- {case WM_CREATE:
- /* create cursors */
- ArrowCursor = LoadCursor(NULL, IDC_ARROW);
- ///WaitCursor = LoadCursor(NULL, IDC_WAIT);
- SetCursor(ArrowCursor);
- #ifdef WIN32
- #else
- /* create thunk for Win16 */
- lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc,hInstance);
- #endif
- /* initialize paint module */
- PaintInit();
-
- /* define diagnostics log file */
- ///seeStringParam(SEE_LOG_FILE, (LPSTR)"from.log");
- /* turn off AUTO CALL driver */
- seeIntegerParam(SEE_AUTO_CALL_DRIVER, 0);
- ShowINI();
- /* verify that we have all strings read in */
- if(lstrlen((LPSTR)Pop3String)==0) DisplayLine("ERROR: Missing POP3 server name.");
- if(lstrlen((LPSTR)UserString)==0) DisplayLine("ERROR: Missing POP3 user name");
- if(lstrlen((LPSTR)PassString)==0) DisplayLine("ERROR: Missing POP3 password");
- break;
-
- case WM_COMMAND:
- switch(wParam)
- {
- case MSG_ABOUT :
- #ifdef WIN32
- DialogBox(hInstance, "AboutBox", hMainWnd, AboutDlgProc);
- #else
- DialogBox(hInstance, "AboutBox", hMainWnd, lpfnAboutDlgProc);
- #endif
- return 0;
-
- case MSG_DEBUG:
- ShowINI();
- break;
-
-
- case MSG_BREAK:
- NextCommand = CMD_NONE;
- seeClose();
- ///SetCursor(ArrowCursor);
- break;
-
- case MSG_EXIT:
- DestroyWindow(hMainWnd);
- break;
-
- case MSG_MAIL_CHK:
- /* set 1st command */
- if(NextCommand==CMD_NONE)
- {/* set 1st command in sequence */
- NextCommand = CMD_CONNECT;
- POST_MSG(CMD_DRIVER);
- }
- break;
- }
- break;
-
- case WM_PAINT:
- HideCaret(hMainWnd);
- hDC = BeginPaint(hMainWnd, &ps);
- SetMapMode(hDC,MM_ANISOTROPIC);
- SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
- PaintMain(hDC,&ps);
- EndPaint(hMainWnd,&ps);
- SetCaretPos(PaintGetColPos(),PaintGetRowPos());
- ShowCaret(hMainWnd);
- break;
-
- case WM_USER: /* posted by POST_MSG */
- /* execute case */
- switch(wParam)
- {case CMD_DRIVER:
- /* execute next driver state */
- Code = seeDriver();
- if(Code<0)
- {DisplayError(Code,"seeDriver:");
- break;
- }
- if(Code>0)
- {POST_MSG(CMD_DRIVER);
- break;
- }
- /* Code==0 (driver is stopped): time to execute next command */
- POST_MSG(NextCommand);
- break;
-
- case CMD_NONE:
- break;
-
- case CMD_CONNECT:
- /* connect to POP3 server */
- DisplayLine((LPSTR)"Connecting...");
- Code = seePop3Connect(
- (LPSTR)Pop3String, /* POP3 server */
- (LPSTR)UserString, /* user */
- (LPSTR)PassString); /* password */
- if(Code<0)
- {DisplayError(Code,"Connect:");
- break;
- }
- /* next command after connecting */
- NextCommand = CMD_READY;
- POST_MSG(CMD_DRIVER);
- break;
-
- case CMD_READY:
- /* connected & ready to proceed */
- DisplayLine((LPSTR)"Getting message count...");
- Code = seeGetEmailCount();
- if(Code<0)
- {DisplayError(Code,"Getting mail:");
- NextCommand = CMD_NONE;
- POST_MSG(CMD_DRIVER);
- break;
- }
- /* next command after requesting message count */
- NextCommand = CMD_GET_COUNT;
- POST_MSG(CMD_DRIVER);
- break;
-
- case CMD_GET_COUNT:
- /* get # messages waiting */
- MessageCount = seeStatistics(SEE_GET_MSG_COUNT);
- wsprintf((LPSTR)Temp,"%d messages waiting.", MessageCount);
- DisplayLine((LPSTR)Temp);
- /* got any messages ? */
- if(MessageCount==0)
- {NextCommand = CMD_CLOSE;
- POST_MSG(CMD_DRIVER);
- break;
- }
- /* initialize for first email message */
- MessageNbr = 0;
- /* ready for first email message */
- NextCommand = CMD_NEXT_MSG;
- POST_MSG(CMD_DRIVER);
- break;
-
- case CMD_NEXT_MSG:
- /* any more email ? */
- if(++MessageNbr>MessageCount)
- {/* all messages have been read */
- NextCommand = CMD_CLOSE;
- POST_MSG(CMD_DRIVER);
- break;
- }
- /* got more email */
- wsprintf((LPSTR)Temp,"---[ Message %d ]------------------------------------",MessageNbr);
- DisplayLine((LPSTR)Temp);
- /* get headers from next email */
- Code = seeGetEmailLines(MessageNbr, 0, (LPSTR)Buffer, BUFF_SIZE);
- if(Code<0)
- {DisplayError(Code,"seeGetEmailLines:");
- NextCommand = CMD_CLOSE;
- POST_MSG(CMD_DRIVER);
- break;
- }
- /* next command after requesting headers for this email message */
- NextCommand = CMD_GET_HEADERS;
- POST_MSG(CMD_DRIVER);
- break;
-
- case CMD_GET_HEADERS:
- /* display "DATE: " line */
- n = seeExtractText((LPSTR)Buffer, "Date: ", (LPSTR)Temp, TEMP_SIZE);
- if(n>0)
- {wsprintf((LPSTR)Temp,"%s", (LPSTR)Temp);
- DisplayString((LPSTR)Temp);
- }
- /* display "FROM: " line */
- n = seeExtractText((LPSTR)Buffer, "From: ", (LPSTR)Temp, TEMP_SIZE);
- if(n>0)
- {wsprintf((LPSTR)Temp,"%s", (LPSTR)Temp);
- DisplayString((LPSTR)Temp);
- }
- /* display "SUBJECT: " line */
- n = seeExtractText((LPSTR)Buffer, "Subject: ", (LPSTR)Temp, TEMP_SIZE);
- if(n>0)
- {wsprintf((LPSTR)Temp,"%s", (LPSTR)Temp);
- DisplayString((LPSTR)Temp);
- }
- /* back for next email message */
- NextCommand = CMD_NEXT_MSG;
- POST_MSG(CMD_DRIVER);
- break;
-
- case CMD_CLOSE:
- /* all done */
- seeClose();
- if(MessageCount>0)
- DisplayLine((LPSTR)"-------------------------------------------------");
- NextCommand = CMD_NONE;
- POST_MSG(CMD_DRIVER);
- break;
- } /* end-switch(NextCommand) */
-
- break;
-
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
-
- default:
- return (DefWindowProc(hMainWnd, iMsg, wParam, lParam));
- }
- return 0;
- }
-
-